
import { isArr } from './isArr'
import { has } from './has'
// 4171
/**
 * 将值转换为属性路径数组
 * castPath('a.b.c'); // -> ['a', 'b', 'c']
   castPath(['a']); // -> ['a']
   castPath('a[0].b'); // -> ['a', '0', 'b']
   castPath('a.b.c', { 'a.b.c': true }); // -> ['a.b.c']
 * @param {*} path 要转换的值
 * @param {*} obj 目标对象
 * @returns 属性路径数组
 */ 
export function castPath (str, obj) {
  var regPropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
  var regEscapeChar = /\\(\\)?/g;
  if (isArr(str)) return str;
  if (obj && has(obj, str)) return [str];
  var ret = [];
  str.replace(regPropName, function (match, number, quote, str) {
    ret.push(quote ? str.replace(regEscapeChar, '$1') : number || match);
  });
  return ret;
}